/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editor.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * A customized Label based on the label used in the flow example. Primary
 * selection is denoted by highlight and focus rectangle. Normal selection is
 * denoted by highlight only. Borrowed from the Flow Editor example
 */
public class EditableLabel extends Label {

	private boolean selected;
	private String type;

	public static String CLASS_NAME = "ClassName";
	public static String PACKAGE_NAME = "ClassName";
	public static String ATTRIBUTE_NAME = "AttributeName";
	public static String OPERATION_NAME = "OperationName";
	public static String MULTIPLICITY_NAME = "DescriptionName";
	public static String ASSOCIATION_NAME = "DescriptionName";
	public static String STATE_NAME = "StateName";
	public static String START_STATE_NAME = "StartStateName";
	public static String END_STATE_NAME = "EndStateName";
	public static String COMPOSITE_STATE_NAME = "CompositeStateName";
	public static String STATE_MACHINE_NAME = "StateMachineName";
	public static String REGION_NAME = "RegionName";

	public EditableLabel(String text, String type) {
		super(text);
		this.setType(type);
		super.setBackgroundColor(ColorConstants.white);
		if (type.equals(CLASS_NAME) || type.equals(PACKAGE_NAME))
			super.setForegroundColor(ColorConstants.black);
		else if (type.equals(ATTRIBUTE_NAME))
			super.setForegroundColor(ColorConstants.darkBlue);
		else if (type.equals(OPERATION_NAME))
			super.setForegroundColor(ColorConstants.darkGreen);
		else if (type.equals(MULTIPLICITY_NAME))
			super.setForegroundColor(ColorConstants.red);
		else if (type.equals(ASSOCIATION_NAME))
			super.setForegroundColor(ColorConstants.black);
	}

	private Rectangle getSelectionRectangle() {
		Rectangle bounds = getTextBounds().getCopy();
		bounds.expand(new Insets(2, 2, 0, 0));
		translateToParent(bounds);
		bounds.intersect(getBounds());
		return bounds;
	}

	/**
	 * sets the text of the label
	 */
	public void setText(String s) {
		super.setText(s);
	}

	/**
	 * paints figure differently depends on the whether the figure has focus or
	 * is selected
	 */
	protected void paintFigure(Graphics graphics) {
		if (selected) {
			graphics.pushState();
			graphics.setBackgroundColor(ColorConstants.menuBackgroundSelected);
			graphics.fillRectangle(getSelectionRectangle());
			graphics.popState();
			graphics.setForegroundColor(ColorConstants.white);
		}
		super.paintFigure(graphics);
	}

	/**
	 * Sets the selection state of this SimpleActivityLabel
	 * 
	 * @param b
	 *            true will cause the label to appear selected
	 */
	public void setSelected(boolean b) {
		selected = b;
		repaint();
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}
}
